// d:\byte\tiktok-applet-v3 - 智慧天气 - 副本\pages\mytickt\mytickt.js
const model = require('../../public/model/env.js');
const serialNumber = model.generateSerialNumbe();
const url = model.api_pro_url(); // 线上环境
const key = model.key();
const appid = model.appid();
const userid = tt.getStorageSync('userid');
Page({
  data: {
    U_Id: userid,
    // lassName: 'containerdark',
    title: '优惠券信息',
    jifen_num: 0,
    quan_num: 0,
  },
  onPullDownRefresh: function () {
    // 更新页面数据
    this.fetchData();
  },
  fetchData: function () {
    var that = this;
    // 模拟请求数据
    setTimeout(() => {
      // 积分券数量返回
      // 积分券数量返回
      const requestUrlquan = `${url}/index.php/api/points_cx.php`;
      const quans = {
        url: requestUrlquan,
        data: {
          // appid: appid,
          key: key,
          user_id: this.data.U_Id,
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        dataType: 'json',
      };
      console.log(quans);
      tt.request({
        ...quans,
        success: (res) => {
          const data = res.data;
          console.log(data.data);
          this.setData({
            jifen_num: data.data.points,
            quan_num: data.data.ticket
          });
        },
        fail: (err) => {
          console.log(err);
        }
      });

      // 获取积分信息

      // 停止下拉刷新
      tt.stopPullDownRefresh();
    }, 1500);
  },
  quan_chang() {
    // 积分券数量返回
    const requestUrlquan_chang = `${url}/index.php/api/points_delete.php`;
    const quan_chang = {
      url: requestUrlquan_chang,
      data: {
        // appid: appid,
        ticket_title: '701',
        key: key,
        user_id: this.data.U_Id,
        Invitee_open_id: serialNumber + this.data.U_Id,
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    console.log(quan_chang);
    tt.request({
      ...quan_chang,
      success: (res) => {
        const data = res.data;
        console.log(data);
        if (res.data.code == 200) {

          this.setData({
            jifen_num: this.data.jifen_num - 200,
            quan_num: this.data.quan_num + 1,
          });
          tt.showToast({
            title: '积分券兑换成功',
            icon: 'loading',
            duration: 2000
          });
        }
        if (res.data.code == 302) {
          tt.showToast({
            title: '积分不足',
            icon: 'none',
            duration: 2000
          });
        }

      },
      fail: (err) => {
        console.log(err);
      }
    });
  },
  onLoad: function (options) {
    // 页面加载,标题设置
    tt.setNavigationBarTitle({
      title: this.data.title,
    });
    tt.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000',
    });
    // 页面加载,标题设置结束
    // 积分券数量返回
    const requestUrlquan = `${url}/index.php/api/points_cx.php`;
    const quans = {
      url: requestUrlquan,
      data: {
        // appid: appid,
        key: key,
        user_id: this.data.U_Id,
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      dataType: 'json',
    };
    console.log(quans);
    tt.request({
      ...quans,
      success: (res) => {
        const data = res.data;
        console.log(data.data);
        this.setData({
          jifen_num: data.data.points,
          quan_num: data.data.ticket
        });
      },
      fail: (err) => {
        console.log(err);
      }
    });

  }
})